﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Infrastructure.Aspect.Constants;
using S3System.BusinessEntities;
using System.Data.SqlClient;

namespace S3System.DataAccess
{
    public class CustomerRepository : Master.EntityCrudRepository<BusinessEntities.Customer>, ICustomerRepository
    {
        public override void Maintenance(Customer item)
        {
            try
            {
                Context.Customers.ApplyChanges(item);
                Context.SaveChanges();
                item.AcceptChanges(); 
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.DataAccessPolicy);
                if (rethrow)
                    throw;
            }    
            
        }

   
        public override BusinessEntities.Customer GetOne(object key)
        {
            Customer cust = null;
            try
            {
                cust = (from c in this.Context.Customers where c.Id == (Guid)key select c).FirstOrDefault();
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.DataAccessPolicy);
                if (rethrow)
                    throw;
            }
            return cust;
        }

   
        public override IEnumerable<BusinessEntities.Customer> GetAll()
        {
            List<Customer> result = new List<Customer>(); 
            try
            {
                result= (from c in this.Context.Customers select c).ToList();
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, ExceptionPolicyNames.DataAccessPolicy);
                if (rethrow)
                    throw;
            }
            return result; 
        }

    }
}
